package university;

import university.exceptions.EmptyStringException;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Class that describes discipline
 * @author anatolii.bondarenko
 */
class Discipline implements Serializable {
    private String name;
    private Teacher teacher;

    /**
     * Discipline description
     * @param n is name of discipline
     * @param t is teacher responsible for this discipline
     */
    public Discipline(String n, Teacher t) throws EmptyStringException {
		name = n;
		teacher = t;
        if (n.trim() == ""){
            throw new EmptyStringException("Discipline name cannot be empty or consist only from spaces");
        }
	}

    /**
     * Gets discipline name
     * @return name of discipline
     */
    public String getDisciplineName(){
		return name;
	}

    /**
     * Gets ONLY the teacher's name
     * @return Teachers name
     */
    public Teacher getTeacher(){
		return teacher;
	}

    /*public void writeObject(ObjectOutputStream os) throws Exception{
        try {
            os.defaultWriteObject();
            os.writeObject(teacher);
        }
        catch (Exception e){e.printStackTrace();}
    }
    public void readObject(ObjectInputStream is) throws Exception{
        try {
            is.defaultReadObject();
            teacher = (Teacher) is.readObject();
        }catch (Exception e){e.printStackTrace();}
    }*/


    public static void main(String[] args) throws Exception{
        Teacher abakumov = new Teacher("Valentin", "Abakumov", 1977, 5, 16);
        Teacher pilinskiy = new Teacher("Vladimir", "Pilinskiy", 1977, 5, 16);
        Teacher lazebniy = new Teacher("Sergey", "Lazebniy", 1977, 5, 16);
        Teacher dovzhenko = new Teacher("Oleksandr", "Dovzhenko", 1977, 5, 16);
        Teacher gumen = new Teacher("Tamara", "Gumen", 1977, 5, 16);
        Teacher makarenko = new Teacher("Vladimir", "Makarenko", 1977, 5, 16);
        Teacher butsenko = new Teacher("Yuriy", "Butsenko", 1977, 5, 16);
        Teacher drozd = new Teacher("Vladimir", "Drozd", 1977, 5, 16);
        Teacher ponomarenko = new Teacher("Lilia", "Ponomarenko", 1977, 5, 16);
        Teacher travnikov = new Teacher("Evgen", "Travnikov", 1977, 5, 16);

        Discipline matanbuts = new Discipline("Math Analysis", butsenko);
        Discipline matandrozd = new Discipline("Math Analysis", drozd);
        Discipline angeombuts = new Discipline("Analytic geometry", butsenko);
        Discipline angeomDrozd = new Discipline("Analytic geometry", drozd);
        Discipline physics = new Discipline("Physics", ponomarenko);
        Discipline electrodynamicsPil = new Discipline("Electrodynamics", pilinskiy);
        Discipline analogElectronics = new Discipline("Analog Electronics", gumen);
        Discipline digitalElectronics = new Discipline("Digital Electronics", makarenko);
        Discipline encryptionTheory = new Discipline("Theory of Encryption", abakumov);
        Discipline electricalCircuitsTheory = new Discipline("Electrical Circuits Theory", gumen);
        Discipline electrodynamicsDovzh = new Discipline("Electrodynamics", dovzhenko);
        Discipline emcPil = new Discipline("Electromagnetic compatibility", pilinskiy);
        Discipline emcDovzh = new Discipline("Electromagnetic compatibility", dovzhenko);
        Discipline sateliteNetworks = new Discipline("Satelite Networks", lazebniy);
        Discipline chemistry = new Discipline("Chemistry", travnikov);

        List<Discipline> disciplineList = new ArrayList<Discipline>(0);
        disciplineList.add(matanbuts);
        disciplineList.add(matandrozd);
        disciplineList.add(angeombuts);
        disciplineList.add(angeomDrozd);
        disciplineList.add(physics);
        disciplineList.add(electrodynamicsPil);
        disciplineList.add(analogElectronics);
        disciplineList.add(digitalElectronics);
        disciplineList.add(encryptionTheory);
        disciplineList.add(electricalCircuitsTheory);
        disciplineList.add(electrodynamicsDovzh);
        disciplineList.add(emcPil);
        disciplineList.add(emcDovzh);
        disciplineList.add(sateliteNetworks);
        disciplineList.add(chemistry);

        File disciplines = new File("disciplines.dat");
        disciplines.createNewFile();
        //write disciplines into a file disciplines dat
        try {
            FileOutputStream fs = new FileOutputStream("disciplines.dat");
            ObjectOutputStream os =new ObjectOutputStream(fs);
            os. writeObject(disciplineList);
            os.flush();
            os.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }

        List<Discipline> disciplineList1 = null;
        try {
            FileInputStream fis = new FileInputStream("disciplines.dat");
            ObjectInputStream ois =new ObjectInputStream(fis);
            disciplineList1 = (List<Discipline>) ois.readObject();
            /*for (Discipline d:disciplineList1){
                d.readObject(ois);
            }*/
            ois.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }

        File teachers = new File("teachers.dat");
        teachers.createNewFile();
        PrintWriter pw = new PrintWriter(teachers);
        for (int i = 0; i<disciplineList1.size(); i++){
            String s = disciplineList1.get(i).getDisciplineName() + " - " + disciplineList1.get(i).getTeacher().getName();
            for(int j= i + 1; j<disciplineList1.size(); j++){
                if (disciplineList1.get(i).getDisciplineName().equals(disciplineList1.get(j).getDisciplineName())){
                    s += ", "+disciplineList1.get(j).getTeacher().getName();
                    disciplineList1.remove(j);
                }
            }
            pw.println(s);
        }
        pw.flush();
        pw.close();
    }
}
